<?php
/**
* 프로세스 핸들링
* /ezadm/index.php
* hamhaja <hamhaja@nextez.co.kr>
* 2008-05-06
**/
/*****************************************************************************
* common header
*****************************************************************************/


$site = explode(".",$_SERVER['HTTP_HOST']);

require_once '../common/inc.header.php';
require_once './inc.menus.php';
require_once './inc.frames.php';
$g_title[] = "관리자시스템";
$g_stylesheets = array();

/*$User->_id = "admin";
$User->_level = "1";
$User->_name = "최고관리자";*/
/*****************************************************************************
* set admin menus
*****************************************************************************/
if ($User->is_login())
{
	//if ($User->is_admin()) g_firewall($g_menus);
}
/*****************************************************************************
* set init
*****************************************************************************/
$g_job		= $App->nvl('g_job');
$g_subjob	= $App->nvl('g_subjob');
/*****************************************************************************
* set menu
*****************************************************************************/
if (!$g_job) $g_job = key($g_menus);
if (!$User->is_admin() && !in_array($g_job, array('help_login')))
{
	$g_job = 'help_login';
	$g_subjob = '';
}
$g_jobs = split('_', $g_job);
//if (!count($g_jobs) || !array_key_exists($g_jobs[0], $g_menus)) $Error->notice(NOT_FIND_MENU);
$g_current_menu = & $g_menus[$g_jobs[0]];
$g_family_menus[] = $g_current_menu;
$g_act_keys = array();
for ($i=1; $i<=3; $i++)
{
	if (!$g_current_menu['childs']) break;
	if ($g_jobs[$i] == '') $g_jobs[$i] = key($g_current_menu['childs']);
	$g_act_keys[] = $g_jobs[$i];
	if ($g_current_menu['childs'][$g_jobs[$i]])
	{
		$g_current_menu = & $g_current_menu['childs'][$g_jobs[$i]];
		$g_family_menus[] = $g_current_menu;
	}
	else break;
}
// 메뉴와 액션을 분리하였다!!
for ($i=count($g_act_keys)+1; $i<count($g_jobs); $i++)
	$g_act_keys[] = $g_jobs[$i];
$g_act_key = implode('_', $g_act_keys);
$g_job = implode('_', $g_jobs);
/*****************************************************************************
* set param
*****************************************************************************/
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
	if ($_GET['g_params'])
	{
		parse_str($_GET['g_params'], $params);
		$_GET = $Array->merge_recursive($_GET, $params);
	}
	if ($g_current_menu['params'])
	{
		parse_str($g_current_menu['params'], $params);
		$_GET = $Array->merge_recursive($_GET, $params);
	}
}
/*****************************************************************************
* set action
*****************************************************************************/
$g_action_path = $App->_dir_admin . '/action/' . $g_jobs[0] . '/inc.' . $g_jobs[1] . '.php';
if (file_exists($g_action_path)) require_once $g_action_path;
else $Error->notice(NOT_FIND_ACTION_FILE, $g_action_path);
if (!array_key_exists(implode('_', $g_act_keys), $g_actions)) $Error->notice(UNDEFINED_JOB, $g_job);
if (!$g_subjob) $g_subjob = key($g_actions[$g_act_key]);
if (!array_key_exists($g_subjob, $g_actions[$g_act_key])) $Error->notice(UNDEFINED_SUBJOB, $g_subjob);
$g_action = & $g_actions[$g_act_key][$g_subjob];
if ($_GET['g_frame']) $g_action['frame'] = $_GET['g_frame'];
if (!$g_action['frame']) $g_action['frame'] = 'default';
if (!$g_action['logic']) $g_action['logic'] = $g_jobs[0] . '/' . $g_act_key;
if (!$g_action['tpl']) $g_action['tpl'] = $g_jobs[0] . '/' . $g_act_key . '_' . $g_subjob;
if ($g_action['subjob']) $g_subjob = $g_action['subjob'];
if ($g_action['params'])
{
	parse_str($g_action['params'], $params);
	$_GET = $Array->merge_recursive($_GET, $params);
}
/*****************************************************************************
* set framework
*****************************************************************************/
// include logic
$logic_path = $App->_dir_admin . '/logic/' . $g_action['logic'] . '.php';
if (!file_exists($logic_path)) $Error->notice(NOT_FIND_LOGIC_FILE, $logic_path);
include_once $logic_path;
// include tempate
if (!$g_action['notpl'])
{
	$g_frame = & $g_frames[$g_action['frame']];
	if ($g_frame['html_header']) include_once $App->_dir_admin . '/frame/html_header/' . $g_frame['html_header'] . '.htm';
    if ($g_frame['page_header']) include_once $App->_dir_admin . '/frame/page_header/' . $g_frame['page_header'] . '.htm';

	$tpl_path = $App->_dir_admin . '/template/' . $g_action['tpl'] . '.htm';
	if (!file_exists($tpl_path)) $Error->notice(NOT_FIND_TPL_FILE, $tpl_path);
	include_once $tpl_path;

	if ($g_frame['page_footer']) include_once $App->_dir_admin . '/frame/page_footer/' . $g_frame['page_footer'] . '.htm';
	if ($g_frame['html_footer']) include_once $App->_dir_admin . '/frame/html_footer/' . $g_frame['html_footer'] . '.htm';
}
/*****************************************************************************
* common footer
*****************************************************************************/
include_once '../common/inc.footer.php';
/**************************************************************************
* 기타함수
**************************************************************************/
// 메뉴에 대한 방화벽
function g_firewall(&$menus)
{
	global $App, $Db, $User;
	if ($User->is_super_admin()) return;

	$App->class_require('Table.ProcessMember');
	$ProcessMember = new ProcessMember(&$Db);
	$auth_menus = $ProcessMember->get_menus($User->get_id());


	foreach ($menus as $key1 => $sub_menus)
	{
		foreach ($sub_menus['childs'] as $key2 => $menu)
		{
			$is_del = true;
			if (!$menu['level'])
			{
				if ($auth_menus[$key1][$key2]) $is_del = false;
			}
			else
			{
				if ($menu['level'] >= $User->get_level())	$is_del = false;
			}

			if ($is_del) unset($menus[$key1]['childs'][$key2]);
		}

		if (!count($menus[$key1]['childs'])) unset($menus[$key1]);
	}
	reset($menus);
}
?>